﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Coolite.Ext.Web;
using ENT.GP.Core.Rol;
using ENT.GP.Core.Core;
using ENT.GP.Core;
using entgp.Code;
using System.IO;

namespace entgp.Usuarios
{
    public partial class Roles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    //StoreRol.DataSource = RolesLookup.GetNameValueList();
                    //StoreRol.DataBind();
                }
            }
            catch (Exception ex)
            {
                Util.RegistarLog(ex.ToString());
            }            
        }
        protected void btnBuscar_Click(object sender, AjaxEventArgs e)
        {
            try
            {
                RolCriterio criterio = RolCriterio.NewRolCriterioRoot();
                criterio.Descripcion = string.Format("{0}%", txtDescripcion.Text);
                criterio.Nombre      = string.Format("{0}%", txtNombreRol.Text);

                StoreRol.DataSource  = RolLista.GetRolLista(criterio);
                StoreRol.DataBind();
            }
            catch { }
        }
        protected void btnNuevo_Click(object sender, AjaxEventArgs e)
        {
            string operacion = e.ExtraParams["Operacion"];
            wucRoles1.Mostrar(operacion,0);
        }
        protected void command_Click(object sender, AjaxEventArgs e)
        {
            int idRol;
            int.TryParse(e.ExtraParams["Id"], out idRol);
            hidIdRol.Text    = e.ExtraParams["Id"];

            string operacion = e.ExtraParams["Operacion"];
            switch (operacion)
            {
                 case "Eliminar":
                     Ext.Msg.Confirm("Aviso", "Esta seguro de eliminar el item",
                         new MessageBox.ButtonsConfig
                         {
                             Yes = new MessageBox.ButtonConfig
                             {
                                  
                                  Handler = "RolAjax.EliminarRol();",
                                  Text    = "Si"
                             },
                             No = new MessageBox.ButtonConfig
                             {
                                 Text = "No"
                             }
                         }).Show();

                     break;
                 case "Editar":
                     wucRoles1.Mostrar(operacion, idRol);
                     break;
             }

        }
        [AjaxMethod]
        public void EliminarRol()
        {
            try
            {
                int idRol;
                string ruta;
                string pathDirect;
                int.TryParse(hidIdRol.Text, out idRol);
                var rolDelete = Rol.GetRol(idRol);
                ruta = string.Format("{0}/resources/arbol{1}.xml", ConfigurationManager.AppSettings.Get("SD"), rolDelete.Codigo);
              
                rolDelete.Delete();
                rolDelete.Save();
                pathDirect = Server.MapPath(ruta);
                if (File.Exists(pathDirect))
                    File.Delete(pathDirect);

                UtilP.MostrarNotificacion("El elemento se elimino correctamente", "Aviso", MessageBox.Icon.INFO);
                GridPanelRoles.DeleteSelected();
            }
            catch (Exception ex)
            {
                Util.RegistarLog(ex.ToString());
                UtilP.MostrarNotificacion("El elemento no se elimino", "Aviso", MessageBox.Icon.ERROR);
            }

        }

    }
}
